package com.zzh.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzh.controller.utils.R;
import com.zzh.mapper.UserMapper;
import com.zzh.mapper.VideoMapper;
import com.zzh.pojo.Order;
import com.zzh.pojo.User;
import com.zzh.pojo.Video;
import com.zzh.service.OrderService;
import com.zzh.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;

/**
 *
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VideoMapper videoMapper;

    /**
     * //
     * @param order
     * @return 1:新生成订单需要扣几分 0:不需要扣积分
     */
    @Override
    public int addOrder(Order order) {
        Integer userId = order.getUserId();
        Integer videoId = order.getVideoId();
        Order dbOrder = orderMapper.selectOneByUserIdAndVideoId(userId, videoId);
        //如果数据库不存在订单就新增
        if (dbOrder == null){
            Date date = new Date();
            String format = DateUtil.format(date, "yyyyMMddHHmmss");
            Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            long salt = snowflake.nextId();
            String orderId = format + String.valueOf(salt);
            order.setOrderId(orderId);
            int orderInsert = orderMapper.insert(order);
            if (orderInsert > 0){
                log.info("订单生成成功:" + order);
                return 1;
            }
        }
        return 0;

    }
    /** 购买视频.生成订单*/
    @Override
    public R BuyVideo(Integer id, HttpSession session) {
        //拿到登录后放到session中的user
        if (session.getAttribute("user-session") == null) {
            return R.error("未登录");
        }
        User sessionUser = (User) session.getAttribute("user-session");
        //只放了邮箱和密码,需要根据邮箱再查一次数据库
        String email = sessionUser.getEmail();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail,email);
        User user = userMapper.selectOne(wrapper);
        Integer userVgold = user.getVgold();
        if (userVgold - 1 < 0) {
            return R.error("积分不足");
        }
        Video video = videoMapper.selectById(id);
        int updateVgold = userMapper.updateById(user);
        if (updateVgold > 0) {
            //开始生成订单
            Order order = new Order();
            order.setUserId(user.getId());
            order.setVideoId(video.getId());
            int check = addOrder(order);
            if (check == 1){
                user.setVgold(userVgold - 1);
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", order.getId());
                map.put("imgPath", video.getImgPath());
                map.put("urlPath", video.getVideoPath());
                map.put("info","购买成功,剩余积分" + user.getVgold());
                log.info("用户:" + user.getEmail() + ",购买了id为:" + video.getId() + ",剩余积分:" + user.getVgold());
                return R.success(map);
            }
            if (check == 0){
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", video.getId());
                map.put("imgPath", video.getImgPath());
                map.put("urlPath", video.getVideoPath());
                map.put("info","已经购买过,本次操作不扣积分,剩余积分" + user.getVgold());
                log.info("用户:" + user.getEmail() + ",购买了id为:" + video.getId() + ",剩余积分:" + user.getVgold());
                return R.success(map);
            }

        }
        return R.error("未知错误");
    }
}




